<template>
	<page-meta :page-style="show ? 'overflow: hidden; touch-action: none;' : ''">
	</page-meta>
	<view class="container">
		<u-popup :show="show" @close="close" mode="bottom" :duration="100"  round="30rpx">
			<view class="container-popup">
				<view class="avatar">
					<image :src="preData.avatar" mode=""></image>
				</view>
				<view class="header-center">
					<view class="nickName">
						{{ preData.nickName }}
					</view>
					<view class="header-center-content">
						感谢您的支持与鼓励~
					</view>
				</view>
				<view class="colse" @click="close">
					<image src="/static/X.png" mode=""></image>
				</view>

				<view class="header-left">
					<view class="header-left-top">
						总蜂蜜值：{{currentHoneySum ? currentHoneySum : 0}}
					</view>
					<view class="header-left-bottom" @click.stop="showRechargeModal = true">
						<view class="left">
							<image src="/static/index/wellet.png" mode=""></image>
						</view>
						<view class="middle">
							充值
						</view>
						<view class="right">
							<u-icon name="arrow-right" size="24rpx" color="rgba(119, 119, 119, 1)"></u-icon>
						</view>
					</view>
				</view>

				<view class="middle-content">
					<view class="honey-options">
						<!-- type=1 的选项 -->
						<view v-for="(item, index) in tipList" v-if="item.type === 1" :key="index" class="honey-item"
							:class="{ 'selected': selectedOption === index, 'standard-honey': item.type === 1, 'custom-honey': item.type === 2 }"
							@click="selectOption(index,item)">
							<view class="honey-img">
								<image v-if="item.type === 1" src="/static/index/wellet-fm.png" mode=""></image>
								<text v-else>自定义</text>
							</view>
							<view class="honey-text">
								<text v-if="item.type === 1">{{ item.num }}蜂蜜</text>
								<text v-else>蜂蜜数量 (1-5000)</text>
							</view>
						</view>

						<!-- type=2 的自定义选项 -->
						<view v-for="(item, index) in tipList" v-if="item.type === 2" :key="index" class="honey-item"
							:class="{ 'selected': selectedOption === index, 'standard-honey': item.type === 1, 'custom-honey': item.type === 2 }"
							@click="selectOption(index,item)">
							<view class="honey-box">
								<view class="honey-img-type2">
									<text>自定义</text>
								</view>
								<view class="honey-text-type2">
									<text>蜂蜜数量 (1-5000)</text>
								</view>
							</view>
						</view>
					</view>
				</view>

				<view class="footer-btn-top">
					<!-- 协议勾选 -->
					<view class="agreement">
						<label class="checkbox-wrapper" @click="toggleCheckbox">
							<view class="custom-checkbox" :class="{ 'checked': selected }">
								<u-icon v-if="selected" name="checkmark" color="#fff" size="12"></u-icon>
							</view>
							<text>我已阅读并同意
								<text class="protocol" @click.stop="onProtocol">《蜂蜜值用户协议》</text>
							</text>
						</label>
					</view>
				</view>

				<view class="footer-btn-wrapper" @click="handleEmit()">
					<view class="footer-btn">
						支持一下作者
					</view>
				</view>
			</view>
			<customize :show="showCustomizeModal" @close="showCustomizeModal = false" @submit="customizeNum">
			</customize>
			<recharge :show="showRechargeModal" @close="closeRecharge"></recharge>
		</u-popup>
	</view>
</template>

<script>
	import {
		getHoneySum,
		getHoneySumTip
	} from '@/request/api.js';
	import customize from "@/components/tipModel/customize";
	import recharge from "@/components/tipModel/recharge";
	export default {
		name: "tipModel",
		components: {
			customize,
			recharge
		},
		props: {
			show: {
				type: Boolean,
				default: false,
			},
			preData: {
				type: Object,
			}
		},
		watch: {
			preData: {
				handler(newVal) {
					if (newVal && Object.keys(newVal).length > 0) {

					}
				},
				immediate: true
			},
			show: {
				handler(newVal) {
					if (newVal) {
						// 当弹窗显示时，默认选中第一个选项
						this.selectedOption = 0;
						this.selectHoneySum = this.tipList[0].num
						this.initPage()
					}
				},
				immediate: true
			}
		},

		data() {
			return {
				tipList: [{
						type: 1,
						num: 1
					},
					{
						type: 1,
						num: 10
					},
					{
						type: 1,
						num: 30
					},
					{
						type: 1,
						num: 50
					},
					{
						type: 1,
						num: 100
					},
					{
						type: 2,
						num: 0 // 自定义选项
					},
				],
				selectedOption: null, // 记录选中的选项
				selected: false,
				showCustomizeModal: false,
				showRechargeModal: false,
				currentHoneySum: null,
				selectHoneySum: null
			};
		},
		created() {},

		methods: {
			initPage() {
				let params = {
					userId: this.$store.state.userInfo.userId,
				}
				getHoneySum(params).then(res => {
					if (res.data.code == 200) {
						this.currentHoneySum = res.data.data | 0
					}
				})
			},
			handleEmit() {
				// 校验是否同意协议
				if (!this.selected) {
					uni.showToast({
						title: '请先阅读并同意协议',
						icon: 'none'
					});
					return;
				}

				// 校验选择的蜂蜜数量
				if (!this.selectHoneySum || this.selectHoneySum <= 0) {
					uni.showToast({
						title: '请选择或输入有效的蜂蜜数量',
						icon: 'none'
					});
					return;
				}

				// 校验蜂蜜数量是否超过用户拥有的总量
				if (this.selectHoneySum > this.currentHoneySum) {
					uni.showToast({
						title: `蜂蜜数量不足，当前仅有${this.currentHoneySum}个`,
						icon: 'none'
					});
					return;
				}

				let params = {
					tipUserId: this.$store.state.userInfo.userId,
					userId: this.preData.userId,
					honeyMoney: this.selectHoneySum,
					postId:this.preData.id
				};


				// 不能自己给自己打赏
				if (params.tipUserId == params.userId) {
					uni.showToast({
						title: '不能给自己打赏',
						icon: 'none'
					});
					return;
				}

				getHoneySumTip(params).then(res => {
					if (res.data.code === 200) {
						uni.showToast({
							title: '打赏成功',
							icon: 'success'
						});
						// 可以在这里添加成功后的处理逻辑，比如关闭弹窗
						setTimeout(() => {
							this.close();
						}, 1000);
					} else {
						uni.showToast({
							title: res.data.msg || '打赏失败',
							icon: 'none'
						});
					}
				}).catch(error => {
					console.error('打赏请求失败:', error);
					uni.showToast({
						title: '网络错误，请重试',
						icon: 'none'
					});
				});
			},
			close() {
				this.$emit('close');
			},
			// 关闭充值弹框重新获取蜂蜜余额
			closeRecharge() {
				this.initPage()
				this.showRechargeModal = false
			},
			selectOption(index, item) {
				this.selectedOption = index;
				this.selectHoneySum = item.num
				if (index == 5) {
					this.showCustomizeModal = true
				}
			},
			customizeNum(item) {
				this.selectHoneySum = item
			},
			// 切换复选框状态
			toggleCheckbox() {
				this.selected = !this.selected;
			},
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		.container-popup {
			width: 100vw;
			min-height: 836rpx;
			position: relative;

			.avatar {
				width: 142rpx;
				height: 142rpx;
				position: absolute;
				top: -44rpx;
				left: 50%;
				transform: translateX(-50%);
				z-index: 10;
				background-color: #fff;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;

				image {
					width: 130rpx;
					height: 130rpx;
					border-radius: 50%;
				}
			}

			.header-center {
				position: absolute;
				top: 98rpx;
				left: 50%;
				transform: translateX(-50%);
				z-index: 10;
				text-align: center;

				.nickName {
					font-size: 30rpx;
					color: rgba(0, 0, 0, 1);
					line-height: 36rpx;
				}

				.header-center-content {
					font-size: 20rpx;
					color: rgba(77, 77, 86, 1);
				}
			}

			.colse {
				width: 80rpx;
				height: 56rpx;
				position: absolute;
				top: 0;
				right: 0;
				z-index: 10;
				background-color: rgba(240, 239, 239, 1);
				text-align: center;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 30rpx;

				image {
					width: 40rpx;
					height: 40rpx;
				}
			}

			.header-left {
				margin: 30rpx 0 0 30rpx;

				.header-left-top {
					height: 28rpx;
					font-size: 24rpx;
					font-weight: 400;
					color: rgba(250, 128, 22, 1);
				}

				.header-left-bottom {
					display: flex;
					align-items: center;
					justify-content: center;
					background-color: rgba(245, 244, 244, 1);
					width: 140rpx;
					text-align: center;
					margin-top: 20rpx;
					height: 40rpx;
					border-radius: 20rpx;

					.left {
						width: 30rpx;
						height: 30rpx;
						display: flex;
						align-items: center;
						justify-content: center;

						image {
							width: 100%;
							height: 100%;
						}
					}

					.middle {
						color: rgba(250, 128, 22, 1);
						font-size: 24rpx;
						line-height: 24rpx;
						margin: 0 10rpx;
					}

					.right {
						width: 30rpx;
						height: 30rpx;
						display: flex;
						align-items: center;
						justify-content: center;
					}
				}
			}

			.middle-content {
				margin-top: 30rpx;
				padding: 30rpx;

				.honey-options {
					display: flex;
					flex-wrap: wrap;
					gap: 20rpx;
					justify-content: space-between;

					.honey-item {
						width: 200rpx;
						height: 180rpx;
						/* 总高度180rpx */
						background-color: #fff;
						border: 2rpx solid #eaeaea;
						border-radius: 12rpx;
						display: flex;
						flex-direction: column;
						cursor: pointer;
						transition: all 0.3s ease;
						padding: 0;
						box-sizing: border-box;

						&.selected {
							border-color: #ff9900;
							box-shadow: 0 0 10rpx rgba(255, 153, 0, 0.3);
							background-color: rgba(240, 189, 123, 0.3);

							.honey-text {
								width: 100%;
								height: 60rpx;
								line-height: 60rpx;
								font-size: 24rpx;
								color: #fff;
								text-align: center;
								background-color: rgba(255, 149, 58, 1);
								margin-top: 0;
							}
						}

						.honey-img {
							height: 120rpx;
							/* 上部分高度120rpx */
							display: flex;
							align-items: center;
							justify-content: center;

							image {
								width: 80rpx;
								height: 80rpx;
								border-radius: 8rpx;
								object-fit: cover;
							}

							text {
								color: rgba(120, 120, 130, 1);
								font-size: 24rpx;
							}
						}

						.honey-text {
							height: 60rpx;
							line-height: 60rpx;
							font-size: 24rpx;
							color: #333;
							text-align: center;
							margin-top: 0;

						}


						.honey-box {
							height: 180rpx;
							display: flex;
							flex-direction: column;
							align-items: center;
							justify-content: center;

							.honey-img-type2 {
								display: flex;
								align-items: center;
								justify-content: center;

								text {
									color: rgba(120, 120, 130, 1);
									font-size: 24rpx;
								}
							}

							.honey-text-type2 {
								height: 60rpx;
								line-height: 30rpx;
								font-size: 24rpx;
								color: #333;
								width: 100rpx;
								display: flex;
								align-items: center;
								justify-content: center;
								word-wrap: break-word;
								word-break: break-all;
								overflow: hidden;
								margin: 0 auto;
								text-align: center;

								text {
									display: inline-block;
									text-align: center;
								}
							}
						}

					}
				}
			}

			.footer-btn-top {
				width: calc(100% - 80rpx);
				position: fixed;
				bottom: 126rpx;
				left: 50%;
				transform: translate(-50%, -20%);
				text-align: center;
				color: #fff;
				padding: 40rpx;

				.agreement {
					display: flex;
					align-items: center;
					justify-content: center;

					.checkbox-wrapper {
						display: flex;
						align-items: center;
						cursor: pointer;
					}

					.custom-checkbox {
						width: 32rpx;
						height: 32rpx;
						border-radius: 50%;
						border: 2rpx solid #ccc;
						display: flex;
						align-items: center;
						justify-content: center;
						margin-right: 16rpx;
						background-color: #fff;
						transition: all 0.3s ease;

						&.checked {
							background-color: rgba(255, 179, 39, 1);
							border-color: rgba(255, 179, 39, 1);
						}
					}

					.protocol {
						color: rgba(255, 179, 39, 1);
					}
				}

				.agreement text {
					font-size: 24rpx;
					color: rgba(120, 120, 130, 1);
				}

			}

			/* 底部按钮固定容器 */
			.footer-btn-wrapper {
				position: fixed;
				bottom: 0;
				left: 0;
				right: 0;
				display: flex;
				justify-content: center;
				padding: 20rpx 0 50rpx;
				background: linear-gradient(to bottom, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1));

				/* 渐变背景 */

				.footer-btn {
					background-color: rgba(255, 179, 39, 1);
					font-weight: 900;
					font-size: 36rpx;
					line-height: 36rpx;
					padding: 30rpx 36rpx;
					text-align: center;
					width: 400rpx;
					border-radius: 40rpx;
					color: rgba(255, 255, 255, 1);
					box-shadow: 0 8rpx 20rpx rgba(255, 179, 39, 0.3);
				}
			}
		}
	}
</style>